const moment = require('moment');
const { Types } = require('mongoose');
const { versionType } = require('../../../project-versions');
const DefaultReporter = require('../default-report/index');

class MyReporter extends DefaultReporter {
  constructor(...args) {
    super(...args);
    this.baseDir = __dirname;
  }

  generateFileReport(data, output) {
    if (this.isIntegrationTest()) {
      data.functions.forEach((func) => {
        func.integrationFunctionNameStr = func.integrationFunctions.reduce((str, item) => str + item.name, '');
        func.tests.forEach((test) => {
          test.indexStr = test.index.toString();
          test.testTime = moment(Types.ObjectId(test.testId).getTimestamp()).format('YYYY/MM/DD HH:mm:ss');
          switch (test.resultKind) {
            case 'succeed':
            case 'nonStrictSucceed':
              test.resultStr = '通过';
              break;
            case 'failed':
              test.resultStr = '未通过';
              break;
            case 'exception':
              test.resultStr = '执行异常';
              break;
            case 'codeDefect':
              test.resultStr = '运行时错误';
              break;
            case 'noResult':
              test.resultStr = '无结果';
              break;
            case 'unknown':
              test.resultStr = '未执行';
              break;
            default:
              test.resultStr = '';
          }
        });
      });
      return this.generateReport('file-template', data, output);
    }
    throw new Error('unit test report not impl, should use default report template');
  }

  generateSummaryReport(data, output) {
    // const baseDir = path.resolve(__dirname, '../default-report');
    return super.generateSummaryReport(data, output);
  }

  static versionTypeSupport(vType) {
    return vType === versionType.integrationTest;
  }
}
module.exports = MyReporter;
